package com.yeswater.es.test;

import com.alibaba.fastjson.JSON;
import com.yeswater.es.test.entity.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

/**
 * author： yeswater
 * create： 2024/4/2
 */
public class ESTest_Doc {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", Integer.parseInt("9200"), "http")));
        
        //batchInsert(esClient);
        //batchDelete(esClient);
        //searchAll(esClient);
        //searchBy(esClient);
        searchBool(esClient);
        esClient.close();
    }
    
    private static void insert(RestHighLevelClient esClient) {
        User user = new User("张三", 20, "男");
        String json = JSON.toJSONString(user);
        
        IndexRequest req = new IndexRequest();
        req.index("user").id("1001").source(json, XContentType.JSON);
    
        try {
            IndexResponse resp = esClient.index(req, RequestOptions.DEFAULT);
            System.out.println(JSON.toJSONString(resp));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    private static void batchInsert(RestHighLevelClient esClient) {
        User user = new User("张三", 20, "男");
        String json = JSON.toJSONString(user);
        
        BulkRequest req = new BulkRequest();
        req.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "qiuzhao", "age", 30));
        req.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "qz", "age", 30));
        req.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "Lucas", "age", 20));
        
        try {
            BulkResponse resp = esClient.bulk(req, RequestOptions.DEFAULT);
            System.out.println(JSON.toJSONString(resp));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    private static void update(RestHighLevelClient esClient) {
        User user = new User("qiuzhao", 20, "男");
        String json = JSON.toJSONString(user);
        
        UpdateRequest req = new UpdateRequest();
        req.index("user").id("1001").doc(json, XContentType.JSON);
        
        try {
            UpdateResponse resp = esClient.update(req, RequestOptions.DEFAULT);
            System.out.println(JSON.toJSONString(resp));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    private static void get(RestHighLevelClient esClient) {
        User user = new User("qiuzhao", 20, "男");
        String json = JSON.toJSONString(user);
        
        GetRequest req = new GetRequest();
        req.index("user").id("1001");
        
        try {
            GetResponse resp = esClient.get(req, RequestOptions.DEFAULT);
            System.out.println(JSON.toJSONString(resp));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    // 全量查询
    private static void searchAll(RestHighLevelClient esClient) {
        SearchRequest req = new SearchRequest();
        req.indices("user");
        req.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
        SearchResponse resp = null;
        try {
            resp = esClient.search(req, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SearchHits hits = resp.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }
    
    // 条件查询
    private static void searchBy(RestHighLevelClient esClient) {
        SearchRequest req = new SearchRequest();
        req.indices("user");
    
        req.source(new SearchSourceBuilder().query(QueryBuilders.matchQuery("age", 30)));
        SearchResponse resp = null;
        try {
            resp = esClient.search(req, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SearchHits hits = resp.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }
    
    // 分页查询
    private static void searchPage(RestHighLevelClient esClient) {
        SearchRequest req = new SearchRequest();
        req.indices("user");
    
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        builder.from(0);
        builder.size(2);
        builder.sort("age", SortOrder.DESC);
        builder.fetchSource();
    
        req.source(builder);
        SearchResponse resp = null;
        try {
            resp = esClient.search(req, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SearchHits hits = resp.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }
    
    // 组合查询
    private static void searchBool(RestHighLevelClient esClient) {
        SearchRequest req = new SearchRequest();
        req.indices("user");
        
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.matchQuery("age", 30));
        //boolQueryBuilder.mustNot(QueryBuilders.matchQuery("age", 30));
        boolQueryBuilder.must(QueryBuilders.matchQuery("name", "qz"));
        builder.query(boolQueryBuilder);
        
        req.source(builder);
        SearchResponse resp = null;
        try {
            resp = esClient.search(req, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SearchHits hits = resp.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }
    
    // 模糊查询
    
    // 最大值查询
    
    private static void delete(RestHighLevelClient esClient) {
    
        DeleteRequest req = new DeleteRequest();
        req.index("user").id("1001");
        
        try {
            DeleteResponse resp = esClient.delete(req, RequestOptions.DEFAULT);
            System.out.println(JSON.toJSONString(resp));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    private static void batchDelete(RestHighLevelClient esClient) {
        
        BulkRequest req = new BulkRequest();
        req.add(new DeleteRequest().index("user").id("1001"));
        req.add(new DeleteRequest().index("user").id("1002"));
        req.add(new DeleteRequest().index("user").id("1003"));
        
        try {
            BulkResponse resp = esClient.bulk(req, RequestOptions.DEFAULT);
            System.out.println(JSON.toJSONString(resp));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
